package com.asset.dispoal.abolishPool.service.common;

import com.asset.dispoal.abolishPool.domain.db.entity.GbAbolishCaseContent;
import com.asset.dispoal.abolishPool.domain.db.mapper.AbolishCaseContentMapper;
import com.asset.dispoal.abolishPool.dto.AbolishPoolResponse;
import com.asset.dispoal.abolishPool.dto.QueryAbolishPoolRequest;
import com.asset.dispoal.abolishPool.service.converter.AbolishConverter;
import com.asset.dispoal.commom.page.PagingQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @description: 数据仓储
 * @author: yf
 * @date: 2022-09-22 15:30:55
 */
@Repository
public class QueryAbolishPoolCommand implements BaseCommand<QueryAbolishPoolRequest, Result<PageVO<AbolishPoolResponse>>> {
    @Autowired
    private AbolishCaseContentMapper abolishCaseContentMapper;

    @Override
    public Result<PageVO<AbolishPoolResponse>> execute(QueryAbolishPoolRequest request) {
        GbAbolishCaseContent caseContent = AbolishConverter.INSTANCE.pageReqToDo(request);
        caseContent.setState("1");
        caseContent.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        Integer count = abolishCaseContentMapper.count(caseContent);
        Page<GbAbolishCaseContent> page = PagingQuery.getPage(count, caseContent);

        PageVO<AbolishPoolResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);
        if (page != null) {
            List<GbAbolishCaseContent> gbAbolishCaseContents = abolishCaseContentMapper.queryPage(page);
            pageVO.setResult(AbolishConverter.INSTANCE.doToRes(gbAbolishCaseContents));
        }
        return Result.ok(pageVO);
    }
}
